<?php
require_once 'common.php';

// 获取请求参数
$category_id = $_GET['category'] ?? 0;
$page = max(1, intval($_GET['page'] ?? 1));

// 调用接口获取产品列表 ?app_id=10001
$params = [
    'category_id' => $category_id,
    'app_id' => 10001,
    'page' => $page,
    'list_rows' => PAGE_SIZE
];
$product_data = api_request(API_PRODUCT_LIST, $params);
$products = $product_data['data']['list']['data'] ?? [];
$total = $product_data['data']['list']['total'] ?? 0;
$total_page = ceil($total / PAGE_SIZE);

// 获取当前分类名称（用于SEO）
$category_name = '全部商品';
$categories = get_categories();
// print_r($categories);
if (!empty($categories['data']['list']) && $category_id) {
    foreach ($categories['data']['list'] as $cate) {
        if ($cate['category_id'] == $category_id) {
            $category_name = $cate['name'];
            break;
        }
    }
}

// SEO标签配置
$page_title = $category_name . ' - ' . SITE_NAME;
$page_desc = "精选{$category_name}共{$total}款，价格优惠，品质保障，支持货到付款";
$page_keywords = "{$category_name},{$category_name}价格,{$category_name}推荐," . SITE_NAME;
$canonical_url = build_url('list', ['category' => $category_id, 'page' => $page]);
?>
<?php include 'templates/header.php'; ?>
<?php include 'templates/navbar.php'; ?>

<!-- 页面标题 -->
<!--
<section class="section page-header">
  <div class="container">
    <h1>我们的产品</h1>
    <p class="text-muted">精选优质益生元产品，满足不同人群需求</p>
  </div>
</section>
-->

<!-- 产品列表 -->
<section class="section product-list">
  <div class="container">
    <!-- 分类筛选 -->
    <div class="categories-filter mb-5">
      <div class="row">
        <div class="col-md-12">
          <div class="btn-group" role="group">
		  <?php if ( $category_id == 0 ): ?>
			  <a href="product_list.php" class="btn btn-primary">全部产品</a>
		  <?php else: ?>
             <a href="product_list.php" class="btn btn-secondary">全部产品</a>
		  <?php endif; ?>
            <?php
            // 获取产品分类
            $categories = get_categories();
            if (!empty($categories['data']['list'])) {
                foreach ($categories['data']['list'] as $category) {
					if ( $category_id == $category['category_id'] ){
						echo '<a href="product_list.php?category=' . $category['category_id'] . '" class="btn btn-primary">' . $category['name'] . '</a>';
					}else{
						echo '<a href="product_list.php?category=' . $category['category_id'] . '" class="btn btn-secondary">' . $category['name'] . '</a>';
					}
                }
            }
            ?>
          </div>
        </div>
      </div>
    </div>

    <!-- 产品网格 -->
    <div class="row">
     <?php if (empty($products)): ?>
          <div class="col-12 text-center py-5"><p>暂无产品数据</p></div>
      <?php else: ?>
		<?php foreach ($products as $product): ?>
			<?php include 'templates/product_item.php'; ?>
		<?php endforeach; ?>
	<?php endif; ?>
    </div>

    <!-- 分页 -->
    <?php if (!empty($total_page) && $total > PAGE_SIZE): ?>
    <nav class="mt-5">
      <ul class="pagination justify-content-center">
        <?php
        
        // 上一页
        if ($page > 1) {
            $prev_url = build_url('product_list', ['category' => $category_id, 'page' => $page - 1]);
            echo '<li class="page-item"><a class="page-link" href="' . $prev_url . '">上一页</a></li>';
        }
        
        // 页码
        for ($i = 1; $i <= $total_page; $i++) {
            $page_url = build_url('product_list', ['category' => $category_id, 'page' => $i]);
            $active = $page == $i ? 'active' : '';
            echo '<li class="page-item ' . $active . '"><a class="page-link" href="' . $page_url . '">' . $i . '</a></li>';
        }
        
        // 下一页
        if ($page < $total_page) {
            $next_url = build_url('product_list', ['category' => $category_id, 'page' => $page + 1]);
            echo '<li class="page-item"><a class="page-link" href="' . $next_url . '">下一页</a></li>';
        }
        ?>
      </ul>
    </nav>
    <?php endif; ?>
  </div>
</section>

<?php include 'templates/footer.php'; ?>